/*
* @Author: wangke
* @Date:   2016-06-02 09:25:53
* @Last Modified by:   wangke
* @Last Modified time: 2016-06-06 13:53:14
*/

#include <stdio.h>
#include "sys/types.h"
#include "sys/socket.h"
#include "string.h"
#include "stdlib.h"
#include "unistd.h"
#include "netinet/in.h"	// struct sockaddr_in 

#define  MYPORT	(8888)




int main(int argc, char const *argv[])
{
	int socket_fd;
	struct sockaddr_in cliAddr;
	int ret;
	char *sbuf="ab23cd";
	char rbuf[256];

	socket_fd = socket(AF_INET, SOCK_STREAM, 0);
	printf("[CLI] socket fd = %d\n", socket_fd);
	if(socket_fd == -1) {
		printf("[CLI] socket err\n");
		exit(1);
	}

	printf("0000");

	cliAddr.sin_family = AF_INET;
	cliAddr.sin_port = htons(MYPORT);
	cliAddr.sin_addr.s_addr = inet_addr("127.0.0.1");

	printf("1111");

	ret = connect(socket_fd, (struct sockaddr *)&cliAddr, sizeof(struct sockaddr_in));
	if(ret == -1) {
		printf("[CLI] connect err\n");
		exit(1);
	}
	else{
		printf("[CLI] connect ok\n");
	}

	while(1) {

		sleep(2);

		memset(rbuf, '\0', sizeof(rbuf));
	    ret = send(socket_fd, sbuf, strlen(sbuf), 0);
	    if(ret>0) {
	    	printf("[CLI] send: %s\n", sbuf);
	    }
	    else{
	    	printf("[CLI] send err, ret = %d\n", ret);
	    }

	    recv(socket_fd, rbuf, sizeof(rbuf), 0);

	    printf("[CLI] recv msg: %s\n", rbuf);
	}

	close(socket_fd);

	return 0;
}
